local card_data = {}
local card_skill = {}
local public_pool = {}
local card_colors = {
    ['N'] = '|cff009900',
    ['R'] = '|cff00ffff',
    ['SR'] = '|cffff00ff',
    ['SSR'] = '|cffff9900',
    ['UR'] = '|cffff0000',
    ['UR+'] = '|cffff0000',
}

for i,v in ipairs(load_excel('卡组设定.xlsx','卡牌技能')) do
    local name = v.name
    ac.lni('skill',name,v)
    local dmg_tip = get_dmgty_tip(v)
    local temp = ac.dummy_unit:add_skill(name,'英雄')
    temp:remove()
    card_skill[name] = ('|cffffff00[%s]|r%s|n%s'):format(name,dmg_tip or '',temp:get_tip())
end


local frames = {
    ['N'] = [[UI\外框\绿.tga]],
    ['R'] = [[UI\外框\蓝.tga]],
    ['SR'] = [[UI\外框\紫.tga]],
    ['SSR'] = [[UI\外框\金.tga]],
    ['UR'] = [[UI\外框\红.tga]],
    ['UR+'] = [[UI\外框\红.tga]],
}

--卡组数据注册
for i,v in pairs(load_excel('卡组设定.xlsx','基础设置')) do
    local name = v.name
    local lv = v['品质']
    v.title = v.title or ('%s%s[%s]'):format(card_colors[lv],name,lv)
    v.art = v.art or [[ReplaceableTextures\CommandButtons\BTNClawsOfAttack.blp]]
    v.skill_type = '抽卡天赋'
    v.frame_art = frames[lv]
    --基础属性/文本
    local base = {}
    for a=1,6 do
        local key = v['属性'..a]
        local num = v['数值'..a]
        if key and num then
            base[key] = num
        end
    end
    v.base_attr = base
    v.base_tip = atts_tip(base)

    --被动文本
    local info1 = {}
    local info2 = {}

    for a=1,4 do
        local abi = v['卡牌技能'..a]
        if abi then
            if card_skill[abi] then
                table.insert(info1,card_skill[abi])
                table.insert(info2,card_skill[abi])
                --table.insert(info2,('|cffffff00[%s]|r'):format(abi))
            else
                print('错误技能',abi)
            end
        end
    end

    local card_class1 = {}
    local card_class2 = {}
    for a=1,4 do
        local key = ('分类%s'):format(a)
        local class = v[key]
        if class then
            if a<=3 then
                card_class1[class] = true
            end
            table.insert(card_class2,class)
        end
    end
    if v['额外介绍'] then
        table.insert(info1,'|n'..v['额外介绍'])
    end
    if v['合成说明'] then
        if card_class1['入口'] then
            table.insert(info1,('|n|cffff9900说明|r：|n%s'):format(v['合成说明']))
            table.insert(info2,('|n|cffff9900说明|r：|n%s'):format(v['合成说明']))
        else
            table.insert(info1,('|n|cffff9900吞噬条件|r：|n%s'):format(v['合成说明']))
        end
    end
    if v['灰字介绍'] then
        table.insert(info1,('|n|cff999999%s|r'):format(v['灰字介绍']))
    end

    local class_tip = table.concat(card_class2,'/')
    v.card_class1 = card_class1
    v.card_class2 = card_class2
    v.abi_tip = table.concat(info1,'|n')
    if #info1>0 then
        v.abi_tip = '|n' .. v.abi_tip .. '|n'
    end
    
    v.abi_tip2 = table.concat(info2,'|n')
    if #info2>0 then
        v.abi_tip2 = '|n' .. v.abi_tip2 .. '|n'
    end

    --总文本
    v.tip = ('%s%s|n%s'):format(v.base_tip,v.abi_tip,v.tip or '')
    v.card_tip = ('%s%s'):format(v.base_tip,v.abi_tip2)
    
    ac.lni('skill',name,v)

    local temp = ac.dummy_unit:add_skill(name,'英雄')
    temp:remove()
    card_data[name] = temp
    if v['初始']==1 then
        table.insert(public_pool,name)
    end
end

function get_card_data(name)
    return card_data[name]
end

ac.enemy_card = ac.dummy_unit:add_skill('空白','英雄')
ac.enemy_card:remove()





--入口添加
local group = {
    '萧火火',
    '爷爷',
    '蓝银草',
    '卫宫巨侠',
    '东汉末年',
    '古神低语',
    '春秋蝉',
    '封神榜',
}
ac.game:event '卡池-添加卡组'(function(_,player,pool)
    local hero = player.hero
    if hero:get_data('新手上路')==nil then
        table.insert(pool,'敏捷')
        table.insert(pool,'力量')
        table.insert(pool,'智力')
        table.insert(pool,'攻击')
        return
    end

    for _,name in ipairs(public_pool) do
        table.insert(pool,name)
    end
    table.insert(pool,'鹰眼')
    
    local count = hero:get_data('入口-计数',0) - hero:get_data('完成-计数',0)
    if hero and count<3 then
        local info = {}
        for _,name in ipairs(group) do
            if hero[name]==nil then
                table.insert(info,name)
            end
        end
        for a=1,3-count do
            local name,id = table.random(info)
            local skill = hero:add_skill(name)
            table.remove(info,id)
            skill:remove()
            hero:set_data('入口-计数',hero:get_data('入口-计数',0) + 1)
        end
    end
    -- --if hero and (hero:get_data('入口-计数',0))<3 then
    --     table.insert(pool,'萧火火')
    --     table.insert(pool,'爷爷')
    --     table.insert(pool,'蓝银草')
    --     table.insert(pool,'卫宫巨侠') 
    --     table.insert(pool,'东汉末年')
    --     table.insert(pool,'古神低语')
    --     table.insert(pool,'春秋蝉')
    --     table.insert(pool,'封神榜')
    --     table.insert(pool,'鹰眼')
        
    -- end
    if hero and (hero:get_data('UR-计数',0) + hero:get_data('UR+-计数',0))>=4 then
        table.insert(pool,'无限手套')
    end
    if hero and hero:find_skill('鸿钧老祖') then
        table.insert(pool,'伊甸园')
    end
end)

function get_allow_card(player)
    local pool = table.copy(public_pool)
    player:notify('卡池-添加卡组',player,pool)
    return pool
end

local odds = {
    ['N'] = 100,
    ['R'] = 100,
    ['SR'] = 100,
    ['SSR'] = 100,
    ['UR'] = 1,
}
local tx_card = {
    ['炼天魔尊'] = 'N',
    ['大爱仙尊'] = 'N',
}

local th = {
    ['UR+'] = 'UR',
    ['SSR+'] = 'SSR',
}
ac.game:event '玩家-开始抽卡'(function(_,player)
    local hero = player.hero
    local Lock = player:get_data('无法获得卡') or {}
    local cf1 = player:get_data('防止重复') or {}
    local cf2 = {}
    local num = 3
    if hero:get_data('新手上路') then
        player:set_data('防止重复',cf2)
    end
    player:set_data('无法获得卡',Lock)


    local info = {}
    for a=1,num do
        local card = {}
        local weight = {}
        local pool = {}
        player:notify('卡池-添加卡组',player,pool)
        
        for i,name in ipairs(pool) do
            local data = card_data[name]
            local class = data.card_class1
            local pz = data['品质']
            pz = th[pz] or pz
            --if Lock[name]==nil and Lock[pz]~=true and cf[name]==nil and (a==1 or a==2 or class['通用']==nil) then
            if Lock[name]==nil and Lock[pz]~=true and cf1[name]==nil and cf2[name]==nil then
                pz = tx_card[name] or pz
                if not card[pz] then
                    card[pz] = {{},odds[pz] or 1}
                    table.insert(weight,card[pz])
                end
                table.insert(card[pz][1],data)
            end
        end
        local res = player:dispatch('卡池-抽卡修改',player,weight)
        if not res and #weight>0 then
            local obj, id1 = table.get_by_weight(weight)
            local ret, id2 = table.random(obj)
            res = ret
            cf2[ret.name] = true
            table.remove(obj,id2)
            if #obj==0 then
                table.remove(weight,id1)
            end
        elseif not res then
            --res = ac.enemy_card
        end
        table.insert(info,res)
    end
    
    if #info==0 then
        table.insert(info,ac.enemy_card)
    end

    player:set_data('当前额外卡组',nil)
    player:set_data('当前卡组',info)
    player:notify('卡池-显示抽卡',player,info)
end)

--选择天赋卡
ac.game:event '玩家-选择抽卡'(function(_,player,index)
    local data1 = player:get_data('当前额外卡组')
    local data2 = player:get_data('当前卡组')
    local data = data1 or data2
    local hero = player.hero
    local temp = data and data[index]
    if data1 then
        player:set_data('当前额外卡组',nil)
    elseif data2 then
        player:set_data('当前卡组',nil)
    end
    if temp and hero then
        local list = hero:get_data('展示天赋表') or {}
        for a=1,12 do
            if list[a]==nil then
                hero:notify('英雄-获得天赋',hero,temp.name)
                return
            end
        end
        hero:notify('英雄-替换抽卡',hero,temp.name)
    end
end)




--测试数据注册
ac.wait(1000,function()
    local test_data = table.copy(load_excel('卡组设定.xlsx','基础设置'))
    ac.game:notify('测试-注册数据','测试天赋',test_data,function(_,player,name)
        local hero = player.hero
        if hero then
            hero:add_skill(name,'隐藏')
        end
    end)
end)